package com.qingge.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qingge.springboot.common.Result;
import com.qingge.springboot.controller.dto.CourseTeacherVo;
import com.qingge.springboot.entity.ClassCourseTeacher;
import com.qingge.springboot.entity.CourseNew;
import com.qingge.springboot.entity.vo.ClassCourseTeacherVo;
import com.qingge.springboot.service.IClassCourseTeacherService;
import com.qingge.springboot.service.ICourseService;
import com.qingge.springboot.service.ISchoolClassService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/class")
public class ClassController {

    @Resource
    private ISchoolClassService service;

    @Autowired
    public IClassCourseTeacherService courseTeacherService;

    @Resource
    public ICourseService courseService;

    @GetMapping
    public Result getClassSchool(){
        return Result.success(service.list());
    }

    @GetMapping("/getCourse/{classId}")
    public Result getClassCourse(@PathVariable("classId")Integer classId){
        List<ClassCourseTeacherVo> classCourseTeachers = courseTeacherService.selectClassCourseTeacher(classId);
        return  Result.success(classCourseTeachers);
    }

    @PostMapping("/saveCourseClass")
    public Result saveCourseClass(@RequestBody List<ClassCourseTeacherVo> courseTeacherVos){
        List<ClassCourseTeacher> list = new ArrayList<>();
        for (ClassCourseTeacherVo courseTeacherVo : courseTeacherVos) {
            if(courseTeacherVo.getTeacherId()!=null){
                Integer courseId = courseTeacherVo.getCourse();
                Integer classId = courseTeacherVo.getClassId();
                Integer teacherId = courseTeacherVo.getTeacherId();
                ClassCourseTeacher classCourseTeacher = new ClassCourseTeacher(courseTeacherVo.getId(), teacherId, courseId, classId);
                courseTeacherService.updateById(classCourseTeacher);
            }
        }
        return Result.success();
    }

    @GetMapping("/course/{teacherId}")
    public Result getCourse(@PathVariable("teacherId") Integer teacherId){
        //查询老师所教授的全部课程
        List<CourseTeacherVo> courseNews = courseService.selectByTeacherId(teacherId);
        return Result.success(courseNews);
    }
}
